package com.hurl.waguji.biz.collect;

import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.hurl.waguji.base.DmbService;
import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dao.HibernateDao;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjHqHexunBO;
import com.hurl.waguji.persistence.JcsjGbjgbThsBO;
import com.hurl.waguji.persistence.JcsjGdThsBO;
import com.hurl.waguji.persistence.JcsjGdrsThsBO;
import com.hurl.waguji.persistence.JcsjGsgkThsBO;
import com.hurl.waguji.persistence.SjProfileBO;

public class ProfileHandler extends BaseCollectHandler {

	@Override
	public void startup(HibernateDao dao) {
	}

	@Override
	public void execute(DmGpBO gp,HibernateDao dao) {
		SjProfileBO profile = new SjProfileBO();
		profile.setGpdm(gp.getGpDm());
		profile.setGpmc(gp.getGpMc());
		profile.setGpdmJ(gp.getGpDmJ());
		profile.setPyjx(gp.getPyjx());
		profile.setGplxMc(DmbService.getGplx(gp.getGplxDm()));
		
		fillGsgk(profile,gp, dao);
		fillHq(profile,gp, dao);
		fillGd(profile, gp, dao);
		fillZdgd(profile,gp,dao);
		fillGbjg(profile,gp,dao);
		
		saveProfile(profile,dao);
	}
	private void fillGbjg(SjProfileBO profile, DmGpBO gp, HibernateDao dao){
		List<JcsjGbjgbThsBO> list = (List<JcsjGbjgbThsBO>)dao.createCriteria(JcsjGbjgbThsBO.class).add(Restrictions.eq("id.gpDm", gp.getGpDm())).addOrder(Order.desc("id.rq")).setFetchSize(1).setReadOnly(true).list();
		if(list.isEmpty()){
			return;
		}
		JcsjGbjgbThsBO gbjg = (JcsjGbjgbThsBO)list.get(0);
		profile.setGbjgGfzs(gbjg.getGfzs());
		profile.setGbjgLastupdate(gbjg.getId().getRq());
		profile.setGbjgSxltg(gbjg.getSxltg());
		profile.setGbjgYssltg(gbjg.getYssltg());
	}
	private void fillZdgd(SjProfileBO profile, DmGpBO gp, HibernateDao dao){
		List<JcsjGdThsBO> list = (List<JcsjGdThsBO>)dao.createCriteria(JcsjGdThsBO.class).add(Restrictions.eq("gpDm", gp.getGpDm())).addOrder(Order.desc("rq")).addOrder(Order.desc("cgs")).setFetchSize(1).setReadOnly(true).list();
		if(list.isEmpty()){
			return;
		}
		JcsjGdThsBO gd = (JcsjGdThsBO)list.get(0);
		profile.setZdgdCgs(gd.getCgs());
		profile.setZdgdGflx(gd.getGflx());
		profile.setZdgdJgmc(gd.getJgmc());
		profile.setZdgdLastupdate(gd.getRq());
		profile.setZdgdZzgbbl(gd.getZzgbbl());
	}
	
	private void fillGd(SjProfileBO profile, DmGpBO gp, HibernateDao dao){
		List<JcsjGdrsThsBO> list = (List<JcsjGdrsThsBO>)dao.createCriteria(JcsjGdrsThsBO.class).add(Restrictions.eq("id.gpDm", gp.getGpDm())).addOrder(Order.desc("id.rq")).setFetchSize(1).setReadOnly(true).list();
		if(list.isEmpty()){
			return;
		}
		JcsjGdrsThsBO gdrs = (JcsjGdrsThsBO)list.get(0);
		profile.setGdGdrs(gdrs.getGdrs());
		profile.setGdRjltg(gdrs.getRjltg());
		profile.setGdLastupdate(gdrs.getId().getRq());
	}
	
	private void saveProfile(SjProfileBO profile, HibernateDao dao){
		if(isExists(profile, dao)){
			dao.update(profile);
		}else{
			dao.save(profile);
		}
	}
	private boolean isExists(SjProfileBO profile,HibernateDao dao){
		return dao.queryBySQL(SqlReader.getSql("SQL12041701", new String[]{profile.getGpdm()})).setReadOnly(true).list().size()>0;
	}
	private void fillHq(SjProfileBO profile, DmGpBO gp, HibernateDao dao){
		List<JcsjHqHexunBO> list = (List<JcsjHqHexunBO>)dao.createCriteria(JcsjHqHexunBO.class).add(Restrictions.eq("id.gpDm", gp.getGpDm())).addOrder(Order.desc("id.rq")).setFetchSize(1).setReadOnly(true).list();
		if(list.isEmpty()){
			return;
		}
		JcsjHqHexunBO hq = (JcsjHqHexunBO)list.get(0);
		profile.setHqCje(hq.getCje());
		profile.setHqCjl(hq.getCjl());
		profile.setHqHs(hq.getHs());
		profile.setHqJk(hq.getJk());
		profile.setHqLastupdate(hq.getId().getRq());
		profile.setHqLb(hq.getLb());
		profile.setHqZd(hq.getZd());
		profile.setHqZdf(hq.getZdf());
		profile.setHqZf(hq.getZf());
		profile.setHqZg(hq.getZg());
		profile.setHqZs(hq.getZs());
		profile.setHqZxj(hq.getZxj());
	}
	private void fillGsgk(SjProfileBO profile,DmGpBO gp, HibernateDao dao){
		JcsjGsgkThsBO gsgk = dao.get(JcsjGsgkThsBO.class, gp.getGpDm());
		if(gsgk==null)return;
		profile.setGsBgdz(gsgk.getBgdz());
		profile.setGsClrq(gsgk.getClrq());
		profile.setGsCym(gsgk.getCym());
		profile.setGsCz(gsgk.getCz());
		profile.setGsDlds(gsgk.getDlds());
		profile.setGsDm(gsgk.getDm());
		profile.setGsDsz(gsgk.getDsz());
		profile.setGsDzyx(gsgk.getDzyx());
		profile.setGsFrdb(gsgk.getFrdb());
		profile.setGsFxjg(gsgk.getFxjg());
		profile.setGsFxsl(gsgk.getFxsl());
		profile.setGsFxsyl(gsgk.getFxsyl());
		profile.setGsFxzql(gsgk.getFxzql());
		profile.setGsGsdj(gsgk.getGsdj());
		profile.setGsGsjj(gsgk.getGsjj());
		profile.setGsGsmc(gsgk.getGsmc());
		profile.setGsGswz(gsgk.getGswz());
		profile.setGsKggd(gsgk.getKggd());
		profile.setGsLastupdate(gsgk.getLastupdate());
		profile.setGsLsyg(gsgk.getLsyg());
		profile.setGsLxdh(gsgk.getLxdh());
		profile.setGsQyMc(gsgk.getQy());
		profile.setGsSjmz(gsgk.getSjmz());
		profile.setGsSrkpj(gsgk.getSrkpj());
		profile.setGsSsbjr(gsgk.getSsbjr());
		profile.setGsSshyMc(gsgk.getSshy());
		profile.setGsSsrq(gsgk.getSsrq());
		profile.setGsYjmz(gsgk.getYjmz());
		profile.setGsYwmc(gsgk.getYwmc());
		profile.setGsYzbm(gsgk.getYzbm());
		profile.setGsZcdz(gsgk.getZcdz());
		profile.setGsZcxs(gsgk.getZcxs());
		profile.setGsZcxs(gsgk.getZcxs());
		profile.setGsZjl(gsgk.getZjl());
		profile.setGsZqjc(gsgk.getZqjc());
		profile.setGsZyyw(gsgk.getZyyw());
	}
	
	@Override
	public void shutdown(HibernateDao dao) {
	}

}
